<template>
  <div>
    <div class="search_page">
      <div class="search_content">
        <img
          src="@/assets/common/search.png"
          class="search_icon"
          @click="searchBtn()"
        />
        <input
          type="text"
          placeholder="请输入关键词"
          class="search_input"
          v-model="content"
          @keyup.enter="searchBtn"
          maxlength="10"
        />
      </div>
      <div class="search_cancel" @click="cancelPage">取消</div>
    </div>
    <div class="search_main">
      <div class="search_title" v-if="historyList && historyList.length > 0">
        历史记录
        <img
          src="@/assets/common/del.png"
          class="del_icon"
          @click="clearHistory"
        />
      </div>
      <div class="word_box">
        <div
          class="search_word"
          @click="changeHistory(item)"
          v-for="item in historyList"
          :key="item"
        >
          {{ item.length > 10 ? item.substr(0, 10) : item }}
        </div>
      </div>
      <div class="search_title">热门搜索</div>
      <div class="word_box">
        <div
          v-for="(item, index) in hotWordList"
          :key="index"
          @click="changeSearch(item.hotSearchWord)"
          class="recommend_list"
        >
          <!-- <span
            :class="
              index == 0
                ? 'firCls'
                : index == 1
                ? 'secCls'
                : index == 2
                ? 'thirdCls'
                : 'otherCls'
            "
            >{{ index + 1 }}</span
          > -->
          <!-- <span> -->
          <span class="search_word">{{
            item.hotSearchWord.length > 8
              ? item.hotSearchWord.substr(0, 8)
              : item.hotSearchWord
          }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import { getHotWord } from "@/api/index.js";

export default {
  data() {
    return {
      content: "",
      hotWordList: [],
      historyList: []
    };
  },
  mounted() {
    this.getHotWords(); //热搜词
    this.historyList = JSON.parse(localStorage.getItem("historyWord"));
  },
  methods: {
    getHotWords() {
      //热搜词
      getHotWord({ num: 10 }).then(res => {
        if (res.data.code == 0) {
          this.hotWordList = res.data.data;
        } else {
          Toast(res.data.msg);
        }
      });
    },
    changeHistory(word) {
      //历史搜索跳转
      this.setHistory(word);
      this.$router.replace({
        name: "resource",
        query: {
          obj: { content: word, resourceType: this.$route.query.resourceType }
        }
      });
    },
    changeSearch(word) {
      //热搜词跳转
      this.setHistory(word);
      this.$router.replace({
        name: "resource",
        query: {
          obj: { content: word, resourceType: this.$route.query.resourceType }
        }
      });
    },
    searchBtn() {
      //搜索按钮--搜索页面
      if (this.content) {
        this.setHistory(this.content);
      }
      this.$router.replace({
        name: "resource",
        query: {
          // y-搜索框
          obj: {
            content: this.content,
            resourceType: this.$route.query.resourceType
          }
          // pageNum: 1,
          // pageSize: 2
        }
      });
    },
    setHistory(obj) {
      //设置搜索历史词
      let arr = JSON.parse(localStorage.getItem("historyWord"));
      if (arr && arr.length > 0) {
        let isRepeat = arr.indexOf(obj);
        if (isRepeat >= 0) {
          //已经存在历史记录了
          arr.splice(isRepeat, 1);
        } else {
          if (arr.length >= 10) {
            //控制数量
            arr.splice(9, 1);
          }
        }
        arr.unshift(obj);
        localStorage.setItem("historyWord", JSON.stringify(arr)); //token
      } else {
        let arr = [];
        arr.unshift(obj);
        localStorage.setItem("historyWord", JSON.stringify(arr)); //token
      }
    },
    clearHistory() {
      //清空历史记录
      localStorage.clear();
      this.historyList = [];
      Toast("清除成功");
    },
    cancelPage() {
      //取消---返回
      this.$router.go(-1);
    }
  }
};
</script>

<style scoped>
.search_page {
  height: 0.88rem;
  display: flex;
  margin-top: 0.3rem;
  overflow: hidden;
}
.search_page .search_content {
  height: 0.66rem;
  flex: 1;
  border-radius: 0.33rem;
  display: flex;
  margin: 0 0.3rem;
  background: #f2f2f2;
  display: flex;
  align-items: center;
}
.search_page .search_input {
  flex: 1;
  height: 0.5rem;
  display: inline-block;
  font-size: 14px;
  border: none;
  outline: none;
  background: #f2f2f2;
  border-radius: 0 0.33rem 0.33rem 0;
}
.search_page .search_icon {
  width: 0.3rem;
  height: 0.3rem;
  float: left;
  margin: 0.2rem;
}
/* input---placeholder---webkit */
::-webkit-input-placeholder {
  /* WebKit browsers */
  color: #b4b9c3;
}
.search_page .search_cancel {
  font-weight: 500;
  color: #333333;
  line-height: 0.66rem;
  margin-right: 0.32rem;
  font-size: 15px;
}
.search_main {
  padding: 0 0.24rem;
}
.search_title {
  font-size: 15px;
  color: #333;
  font-weight: bold;
  margin-top: 0.46rem;
  clear: both;
}
.search_title .del_icon {
  height: 0.32rem;
  width: 0.32rem;
  float: right;
  margin-right: 0.24rem;
}
.word_box {
  margin-top: 0.3rem;
  clear: both;
  overflow: hidden;
  margin-bottom: 0.8rem;
}
.word_box .history_word {
  padding: 0.2rem 0.28rem;
  border: solid 1px #e2e2e2;
  border-radius: 0.06rem;
  margin-right: 0.24rem;
  margin-bottom: 0.26rem;
  float: left;
}
.word_box .search_word {
  padding: 0 0.28rem;
  color: #555;
  background: #f3f3f3;
  border-radius: 0.06rem;
  margin-right: 0.24rem;
  margin-bottom: 0.26rem;
  float: left;
  height: 0.8rem;
  line-height: 0.8rem;
  font-size: 14px;
}
.word_box .recommend_list {
  line-height: 0.75rem;
  color: #333333;
  font-weight: 500;
  font-size: 14px;
}
.word_box text {
  margin-right: 0.18rem;
  font-size: 16px;
  font-weight: 500;
}
.firCls {
  color: #ff4713;
}
.secCls {
  color: #f86442;
}
.thirdCls {
  color: #ec8710;
}
.otherCls {
  color: #aaaaaa;
}
</style>
